<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>菜单管理</title>
    <link rel="stylesheet" href="../../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../assets/module/admin.css?v=317"/>
    <link rel="stylesheet" href="../../../assets/module/treeTable/treeTable.css?v=317"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">菜单名称:</label>
                        <div class="layui-input-inline">
                            <input name="authorityName" class="layui-input" placeholder="输入菜单名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">菜单路由:</label>
                        <div class="layui-input-inline">
                            <input name="menuUrl" class="layui-input" placeholder="输入路由地址"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="authoritiesTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button id="authoritiesAddBtn" class="layui-btn icon-btn" type="button">
                            <i class="layui-icon">&#xe654;</i>添加
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="authoritiesTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="authoritiesTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="authoritiesEditDialog">
    <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
          style="padding-right: 20px;">
        <input name="authorityId" type="hidden"/>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item" hidden>
                    <input name="authorityId" placeholder="请输入菜单名称" class="layui-input"/>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单</label>
                    <div class="layui-input-block">
                        <div id="authoritiesEditParentSel" class="ew-xmselect-tree"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">菜单名称:</label>
                    <div class="layui-input-block">
                        <input name="authorityName" placeholder="请输入菜单名称" class="layui-input"
                               lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">菜单类型:</label>
                    <div class="layui-input-block">
                        <input name="menuType" type="radio" value="0" title="菜单" checked/>
                        <input name="menuType" type="radio" value="1" title="按钮"/>
                        <input name="menuType" type="radio" value="2" title="接口"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单图标:</label>
                    <div class="layui-input-block">
                        <input name="menuIcon" placeholder="请输入" class="layui-input"/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单url:</label>
                <div class="layui-input-block">
                    <input name="menuUrl" placeholder="请输入菜单url" class="layui-textarea"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script type="text/javascript" src="../../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../../assets/js/common.js?v=317"></script>
<script type="text/javascript" src="../../../assets/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../../../assets/js/js.cookie.min.js"></script>
<script type="text/javascript" src="../../../assets/js/sqjx.js"></script>
<script>
    layui.use(['layer', 'form', 'admin', 'treeTable', 'util', 'xmSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var util = layui.util;
        var xmSelect = layui.xmSelect;
        var tbDataList = [];

        // 渲染表格
        var insTb = treeTable.render({
            elem: '#authoritiesTable',
            tree: {
                iconIndex: 1,
                idName: 'authorityId',
                pidName: 'parentId',
                isPidData: true
            },
            cellMinWidth: 100,
            cols: [
                {type: 'numbers'},
                {field: 'authorityName', title: '菜单名称', width: 300},
                {field: 'authorityId', title: '菜单编号', width: 300},
                {
                    field: 'iconCls',
                    title: '菜单图标',
                    templet: '<p><i class="layui-icon {{d.menuIcon}}"></i></p>',
                    align: 'center',
                    width: 100
                },
                {field: 'menuUrl', title: '菜单url',width:200},
                {title: '操作', templet: '#authoritiesTbBar', singleLine: false, align: 'center'}
            ],
            reqData: function (data, callback) {
                $.post('/office/menu/queryMenuList', function (res) {
                    res.data = res.obj;
                    res.data.forEach(item => {
                        item.authorityName = item.text;
                        item.authorityId = item.id;
                        item.parentId = item.pId;
                        item.menuIcon=item.iconCls;
                        item.menuUrl = item.url;
                        item.orderNumber = item.order;//菜单排序可补充
                        item.open=true;
                        item.menuType=item.menuType
                        item.menuCode=item.menuCode
                    });
                    tbDataList = res.data ? res.data : [];
                    callback(res.data);
                });
            }
        });

        /* 表格操作列点击事件 */
        treeTable.on('tool(authoritiesTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格搜索 */
        form.on('submit(authoritiesTbSearch)', function (data) {
            doTbSearch(data.field, 'authorityId');
            return false;
        });

        /* 添加 */
        $('#authoritiesAddBtn').click(function () {
            showEditModel();
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加') + '菜单',
                content: $('#authoritiesEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('authoritiesEditForm', mData);
                    // 表单提交事件
                    form.on('submit(authoritiesEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        let param = {
                            menuIcon: data.field.menuIcon,
                            menuName: data.field.authorityName,
                            pId: insXmSel.getValue('valueStr'),
                            menuUrl: data.field.menuUrl,
                            menuId: data.field.authorityId,
                            menuCode: data.field.menuCode,
                            menuType: data.field.menuType

                        };
                        $.post(mData ? '/office/menu/updateMenu' : '/office/menu/addMenu', param, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.refresh();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                    // 渲染下拉树
                    var insXmSel = xmSelect.render({
                        el: '#authoritiesEditParentSel',
                        height: '250px',
                        data: insTb.options.data,
                        initValue: mData ? [mData.parentId] : [],
                        model: {label: {type: 'text'}},
                        prop: {
                            name: 'authorityName',
                            value: 'authorityId'
                        },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: true
                        }
                    });
                    // 弹窗不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.authorityName + '”菜单吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('/office/menu/removeMenu', {menuId: obj.data.authorityId}, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.refresh();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 搜索表格数据 */
        function doTbSearch(field, idName) {
            var ids = [], isClear = true;
            for (var i = 0; i < tbDataList.length; i++) {
                var item = tbDataList[i], flag = true;
                for (var f in field) {
                    if (!field.hasOwnProperty(f)) continue;
                    if (!field[f]) continue;
                    isClear = false;
                    if (!item[f] || item[f].indexOf(field[f]) === -1) {
                        flag = false;
                        break;
                    }
                }
                if (flag) ids.push(item[idName]);
            }
            if (isClear) {
                insTb.clearFilter();
            } else {
                insTb.filterData(ids);
            }
        }

    });
</script>
</body>
</html>
